package com.huanhong.hik.util.callback;

import cn.hutool.core.util.StrUtil;
import com.huanhong.hik.bean.Result;
import com.huanhong.hik.socket.SocketIoClient;
import com.huanhong.hik.util.HCNetSDK;
import com.sun.jna.Pointer;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@NoArgsConstructor
public class FRemoteCfgCallBackFaceSet implements HCNetSDK.FRemoteConfigCallback {


    @Override
    public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData) {
        log.info("长连接回调获取数据,NET_SDK_CALLBACK_TYPE_STATUS:{}", dwType);
        switch (dwType) {
            case 0:// NET_SDK_CALLBACK_TYPE_STATUS
                HCNetSDK.REMOTECONFIGSTATUS_CARD struCfgStatus = new HCNetSDK.REMOTECONFIGSTATUS_CARD();
                struCfgStatus.write();
                Pointer pCfgStatus = struCfgStatus.getPointer();
                pCfgStatus.write(0, lpBuffer.getByteArray(0, struCfgStatus.size()), 0, struCfgStatus.size());
                struCfgStatus.read();

                int iStatus = 0;
                for (int i = 0; i < 4; i++) {
                    int ioffset = i * 8;
                    int iByte = struCfgStatus.byStatus[i] & 0xff;
                    iStatus = iStatus + (iByte << ioffset);
                }

                switch (iStatus) {
                    case 1000:// NET_SDK_CALLBACK_STATUS_SUCCESS
                        log.info("下发人脸参数成功,dwStatus:{}", iStatus);
                        if (null != SocketIoClient.socket) {
                            SocketIoClient.socket.emit("setFace", Result.success().toJsonString());
                        }
                        break;
                    case 1001:
                        log.info("正在下发人脸参数中,dwStatus:{}", iStatus);
                        break;
                    case 1002:
                        int iErrorCode = 0;
                        for (int i = 0; i < 4; i++) {
                            int ioffset = i * 8;
                            int iByte = struCfgStatus.byErrorCode[i] & 0xff;
                            iErrorCode = iErrorCode + (iByte << ioffset);
                        }
                        log.info("下发人脸参数失败, dwStatus:{},错误号:{}", iStatus, iErrorCode);
                        if (null != SocketIoClient.socket) {
                            SocketIoClient.socket.emit("setFace", Result.fail("4000", StrUtil.format("下发人脸参数失败, dwStatus:{},错误号:{}", iStatus, iErrorCode)).toJsonString());
                        }
                        break;
                }
                break;
            case 2:// 获取状态数据
                HCNetSDK.NET_DVR_FACE_PARAM_STATUS mStruFaceStatus = new HCNetSDK.NET_DVR_FACE_PARAM_STATUS();
                mStruFaceStatus.write();
                Pointer pStatusInfo = mStruFaceStatus.getPointer();
                pStatusInfo.write(0, lpBuffer.getByteArray(0, mStruFaceStatus.size()), 0, mStruFaceStatus.size());
                mStruFaceStatus.read();
                String str = new String(mStruFaceStatus.byCardNo).trim();
                log.info("下发人脸数据关联的卡号:{},人脸读卡器状态:{},错误描述:{}", str, mStruFaceStatus.byCardReaderRecvStatus[0], new String(mStruFaceStatus.byErrorMsg).trim());
                if (null != SocketIoClient.socket) {
                    SocketIoClient.socket.emit("setFace", Result.fail("4000", StrUtil.format("下发人脸数据关联的卡号:{},人脸读卡器状态:{},错误描述:{}", mStruFaceStatus.byCardReaderRecvStatus[0], new String(mStruFaceStatus.byErrorMsg).trim())).toJsonString());
                }
            default:
                break;
        }
    }
}
